package com.muster.web.controller.common;

import com.muster.common.core.domain.AjaxResult;
import com.muster.logic.DbLogicService;
import com.muster.logic.model.ProcedureResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Description
 * <p>
 * </p>
 * DATE 2020-07-25.
 *
 * @author 刘江涛.
 */
@RestController
@Api(value = "公共操作管理", description = "公共操作管理", tags = {"公共操作管理"})
@RequestMapping("/api/pipeline")
public class ApiCommonController {

    @Autowired
    private DbLogicService dbLogicService;

    @ApiOperation("get请求")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "procName", value = "存储过程名称", dataType = "String"),
            @ApiImplicitParam(name = "params", value = "需要的参数", dataType = "Map")
    })
    @GetMapping
    public AjaxResult get(@RequestParam String procName, @RequestBody Map<String, Object> params) {
        ProcedureResult result = dbLogicService.exec(procName, params);
        return AjaxResult.success(result.getResult());
    }

    @ApiOperation("post请求")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "procName", value = "存储过程名称", dataType = "String"),
            @ApiImplicitParam(name = "params", value = "需要的参数", dataType = "Map")
    })
    @PostMapping
    public AjaxResult post(@RequestParam String procName, @RequestBody Map<String, Object> params) {
        ProcedureResult result = dbLogicService.exec(procName, params);
        return AjaxResult.success(result.getResult());
    }

    @ApiOperation("delete请求")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "procName", value = "存储过程名称", dataType = "String"),
            @ApiImplicitParam(name = "params", value = "需要的参数", dataType = "Map")
    })
    @DeleteMapping
    public AjaxResult delete(@RequestParam String procName, @RequestBody Map<String, Object> params) {
        ProcedureResult result = dbLogicService.exec(procName, params);
        return AjaxResult.success(result.getResult());
    }

    @ApiOperation("put请求")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "procName", value = "存储过程名称", dataType = "String"),
            @ApiImplicitParam(name = "params", value = "需要的参数", dataType = "Map")
    })
    @PutMapping
    public AjaxResult put(@RequestParam String procName, @RequestBody Map<String, Object> params) {
        ProcedureResult result = dbLogicService.exec(procName, params);
        return AjaxResult.success(result.getResult());
    }
}
